package org.fsm.constants;

import com.google.common.collect.Lists;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: CreditStatusEnum
 * @Description: 信贷流程状态机
 * @Author: Donaldhan
 * @Date: 2018-05-15 17:27
 */
public enum CreditStatusEventEnum {
    CHECK_PASS(CreditStatusEnum.START, CreditStatusEnum.CHECK),
    AUDIT_PASS(CreditStatusEnum.CHECK, CreditStatusEnum.AUDIT),
    APPROVE_PASS(CreditStatusEnum.AUDIT, CreditStatusEnum.APPROVE),
    CONTACT_PASS(CreditStatusEnum.APPROVE, CreditStatusEnum.CONTACT),
    LOAN_PASS(CreditStatusEnum.CONTACT, CreditStatusEnum.LOAN),
    CREDIT_FAIL(Arrays.asList(CreditStatusEnum.START, CreditStatusEnum.CHECK
            ,CreditStatusEnum.AUDIT, CreditStatusEnum.APPROVE, CreditStatusEnum.CONTACT),CreditStatusEnum.END_FAIL);

    private final List<CreditStatusEnum> fromStatusList;
    private final CreditStatusEnum toStatus;

    CreditStatusEventEnum(List<CreditStatusEnum> fromStatusList, CreditStatusEnum toStatus) {
        this.fromStatusList = fromStatusList;
        this.toStatus = toStatus;
    }
    CreditStatusEventEnum(CreditStatusEnum fromStatusList, CreditStatusEnum toStatus) {
        this.fromStatusList = Arrays.asList(fromStatusList);
        this.toStatus = toStatus;
    }

    /**
     * 现态是否可以转化为次态，如果可以，则执行响应的操作
     * @param status
     * @return
     */
    public boolean canFire(CreditStatusEnum status){
        for(CreditStatusEnum fromStatus: fromStatusList){
             if(fromStatus == status){
                 return  true;
             }
        }
        return false;
    }

    /**
     * 现态是否可以转化为次态，如果可以，则执行响应的操作
     * @param code
     * @return
     */
    public boolean canFire(String code){
        for(CreditStatusEnum fromStatus: fromStatusList){
            if(fromStatus.getCode().equals(code)){
                return  true;
            }
        }
        return false;
    }
    public List<CreditStatusEnum>  getFromStatusList() {
        return fromStatusList;
    }

    public CreditStatusEnum getToStatus() {
        return toStatus;
    }
}
